--
-- Author: ping.wang
-- Date: 2018-07-10 17:27:22
--
-- AuctionStorageIconListRender
--

local IconRender = require("app.widget.renders.IconRender")
local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionStorageIconListItem))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel_3:getContentSize() )

    self._items = {}
    for i = 1, 5 do
        local render = IconRender.create(self._ui["icon_" .. i])
        render:setClickHandler(function()
            if self._data and self._data[2] then
                local tabId = app:getInst("AuctionController"):getCurrentSelectTabIdx()
                local data = render:getData()
                if data:getAcType() ~= tabId then
                    if data:getAcType() == Enums.AuctionProductType.normal then
                        display.pushToast(L("auctionshopnotice1"))
                    elseif data:getAcType() == Enums.AuctionProductType.bid then
                        display.pushToast(L("auctionshopnotice2"))
                    end
                else
                    executeFunc(self._onClick, "showSoldWin", render:getData())
                end
            end 
            render:setSelected(false)
        end)
        render:setTipEnable(false)
        render:setClickEnable(true)

        self._items[i] = render
    end
    self:refresh()
end

function ClassRef:refresh()
    if self._data and self._data[2] then
        local itemDatas = self._data[2]
        self:setVisible( true )
        local itemNum = #itemDatas
        for i = 1, 5 do
            local render = self._items[i]
            if i > itemNum then
                render:setVisible(false)
            else
                render:setVisible(true)
                --根据数据显示iconRender
                render:setData(itemDatas[i])
                local tabId = app:getInst("AuctionController"):getCurrentSelectTabIdx()
                render:setMaskVisible(itemDatas[i]:getAcType() ~= tabId)
            end
        end

    else
        self:setVisible( false )
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
